package com.mozz.finally_work.servlet.api;

import com.mozz.finally_work.dao.CollectionDao;
import com.mozz.finally_work.dao.SubscribeDao;
import com.mozz.finally_work.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.UUID;

@WebServlet(name = "CollectionBlogServlet", value = "/CollectionBlogServlet")
public class CollectionBlogServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*获取参数*/
        String userId = request.getParameter("userId");
        String blogId = request.getParameter("blogId");
        Boolean flag = Boolean.valueOf(request.getParameter("flag"));
        /*flag为true表示已收藏*/
        SqlSession session = MybatisUtils.getSession();
        CollectionDao dao = session.getMapper(CollectionDao.class);
        Integer result = 0;
        if(flag){
            result = dao.unCollectionBlog(userId, blogId);
        }else{
            result = dao.collectionBlog(UUID.randomUUID().toString(), userId, blogId);
        }
        session.commit();
        session.close();
        response.setHeader("Content-Type", "application/json; charset=UTF-8");
        if (result > 0) {
            response.getWriter().write("success");
        }else{
            response.getWriter().write("error");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
